<%@ page import="dao.CartsDAO" %>
<%@ page import="dao.ProductDAO" %>
<%@ page import="entity.Carts" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    int pid = Integer.parseInt(request.getParameter("pid"));
    int uid = Integer.parseInt(request.getParameter("uid"));
    int count = Integer.parseInt(request.getParameter("count"));
    CartsDAO cartsDAO = new CartsDAO();
    // 查询购物车中有没有此商品
    Carts carts = cartsDAO.queryCartsByProductAndUser(pid,uid);
    int result = 0;
    if ( carts != null ) {
        result = cartsDAO.modifyCartsCount(count, carts.getId());
    } else {
        // 调用数据库方法
        result = cartsDAO.addCartsById(pid, uid, count);
    }
    if ( result > 0 ) {
        ProductDAO productDAO = new ProductDAO();
        int number = productDAO.modifyGoodsNumber( count, pid );
        if ( number > 0 ) {
            response.sendRedirect("carts.jsp");
        } else {
            out.println("购买失败");
        }
    } else {
        out.println("购买失败！");
    }
%>